# vim:ts=4:sw=4:noet:
""" App Entrypoint """

import sys
import wx
import re
import traceback
import c.base.log as log
from c.ui.wxw.mainwindow import MainWindow

log.initLogging()

class BaseApp(wx.PySimpleApp):

	def BringWindowToFront(self):
		try:
			self.GetTopWindow().Raise()
		except:
			pass

	def OnActivate(self, event):
		if event.GetActive():
			self.BringWindowToFront()
		event.Skip()

	def MacReopenApp(self):
		self.BringWindowToFront()

class MainApp:

	def __init__(self, args):
		self.app = BaseApp()
		self.mainFrame = MainWindow(args)

	def excepthook(self, type, value, tb):
		msg = ''.join(traceback.format_exception(type, value, tb))
		msg = re.sub("\".*/c/","\"c/",msg)
		msg = re.sub("\".*\\\\c\\\\","\"c\\\\",msg)
		f = open("fail.log", "wb")
		f.write(msg)
		f.close()
		dial = wx.MessageDialog(self.mainFrame,
				"%s\nEverything may be going horribly awry; keep going?" % msg,
				"Caught Unhandled Exception",
				style = wx.YES_NO|wx.ICON_QUESTION)
		val = dial.ShowModal()
		if val == wx.ID_NO:
			sys.exit(1)

	def run(self):
		sys.excepthook = self.excepthook
		self.app.MainLoop()

